<?php
class ControllerComisiones extends Controller {
	var $error = array();

	function index() {
		$response =& $this->locator->get('response');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template'); 
		$module   =& $this->locator->get('module');

		$template->set('title','Comisiones');

		$template->set('content', $this->getList());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function insert() 
	{
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

		$template->set('title', 'Comisiones');

		if (($request->isPost()) && ($this->validateForm())) 
		{
		
			$sql = "insert into comisiones set descripcion ='?', cod_materia='?', cupomaximo='?'";
			$database->query($database->parse($sql,$request->get('descripcion', 'post'),$request->get('cod_materia', 'post'),$request->get('cupomaximo', 'post')));

			$cache->delete('comisiones');

			$session->set('message', 'Se ha actualizado la tabla de Comisiones');
			
			$response->redirect($url->ssl('comisiones'));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function update() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');


		$template->set('title', 'Comisiones');

		if (($request->isPost()) && ($this->validateForm())) {
			$sql = "update comisiones set descripcion = '?', cod_materia ='?' where cod_comision = '?'";
			$database->query($database->parse($sql, $request->get('descripcion', 'post'),$request->get('cod_materia', 'post'), $request->get('cod_comision', 'post')));

			$cache->delete('comisiones');
			
			$session->set('message', 'Se ha actualizado la tabla Comisiones');
			
			$response->redirect($url->ssl('comisiones'));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function delete() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');


		$template->set('title', 'Comisiones');

		if (($request->get('cod_comision')) && ($this->validateDelete())) {
			$database->query("delete from comisiones where cod_comision = '" . $request->get('cod_comision','get') . "'");

			$cache->delete('comisiones');
			
			$session->set('message', 'Se ha actualizado la tabla Comisiones');
			
			$response->redirect($url->ssl('comisiones'));
		}

		$template->set('content', $this->getList());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function getList() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$config   =& $this->locator->get('config');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$session  =& $this->locator->get('session');
		$user  =& $this->locator->get('user');
		$view = $this->locator->create('template');		

    	if ($session->get('comisiones.cod_comision'))
		{
			$view->set('cod_comision', $session->get('comisiones.cod_comision'));
		}
		else
		{
    		$view->set('cod_comision', '');
		}

		$cols = array();

		$cols[] = array(
			'name'  => 'Codigo',
			'sort'  => 'cod_comision',
			'align' => 'left'
		);

		$cols[] = array(
			'name'  => 'Comisi&oacute;n',
			'sort'  => 'descripcion',
			'align' => 'left'
		);

                $cols[] = array(
      		'name'  => 'Acciones',
      		'align' => 'right'
    	);

		
	
		$sort = array(
			'cod_comision',
			'descripcion',
		);

		if (!$session->get('comisiones.cod_comision'))
		{
			$sql = "select cod_comision, descripcion from comisiones";
		}
		else
		{
			$sql = "select cod_comision, descripcion from comisiones where cod_comision like '?' or descripcion like '?'";
		}
		
		if (in_array($session->get('comisiones.sort'), $sort)) {
			$sql .= " order by " . $session->get('comisiones.sort') . " " . (($session->get('comisiones.order') == 'desc') ? 'desc' : 'asc');
		} else {
			$sql .= " order by cod_comision asc";
		}

		$results = $database->getRows($database->splitQuery($database->parse($sql, '%' . $session->get('comisiones.cod_comision') . '%', '%' . $session->get('comisiones.cod_comision') . '%'), $session->get('comisiones.page'), $config->get('config_max_rows')));
		
		$rows = array();

		foreach ($results as $result) {
			$cell = array();

			$cell[] = array(
				'value'   => @$result['cod_comision'],
				'align'   => 'left',
				'default' => 0
			);

			$cell[] = array(
				'value'   => @$result['descripcion'],
				'align'   => 'left',
				'default' => 0
			);

            $action = array();
      		
			if ($user->getPERMISOS($user->getPERSONA(),'comisiones','M')) {
			$action[] = array(
        		'icon' => 'update.png',
				'text' => $language->get('button_update'),
				'href' => $url->ssl('comisiones', 'update', array('cod_comision' => $result['cod_comision']))
      		);
			}

			if ($user->getPERMISOS($user->getPERSONA(),'comisiones','B')) {
			$action[] = array(
        		'icon' => 'delete.png',
				'text' => $language->get('button_delete'),
				'href' => $url->ssl('comisiones', 'delete', array('cod_comision' => $result['cod_comision']))
      		);
			}
			
      		$cell[] = array(
        		'action' => $action,
        		'align'  => 'right'
      		);
			
			$rows[] = array('cell' => $cell);
		}



		$view->set('heading_title', 'Comisiones');
		$view->set('heading_description','Gesti&oacute;n de Comisiones');

		$view->set('text_default', $language->get('text_default'));
		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

		$view->set('entry_page', $language->get('entry_page'));
		$view->set('entry_search', $language->get('entry_search'));
		$view->set('button_search', $language->get('button_search'));

		$view->set('button_list', $language->get('button_list'));
		$view->set('button_insert', $language->get('button_insert'));
		$view->set('button_exportar', $language->get('button_exportar'));
		
		$view->set('error', @$this->error['message']);

		$view->set('message', $session->get('message'));
		
		$session->delete('message');
		
		$view->set('action', $url->ssl('comisiones', 'page'));

		$view->set('sort', $session->get('comisiones.sort'));
		$view->set('order', $session->get('comisiones.order'));
		$view->set('page', $session->get('comisiones.page'));
                $view->set('search', $session->get('comisiones.search'));
 
		$view->set('cols', $cols);
		$view->set('rows', $rows);

		$view->set('list', $url->ssl('comisiones'));

		if ($user->getPERMISOS($user->getPERSONA(),'comisiones','A')) {$view->set('insert', $url->ssl('comisiones', 'insert'));}

		if ($user->getPERMISOS($user->getPERSONA(),'comisiones','C'))  $view->set('export', $url->ssl('comisiones','exportar'));

		$view->set('titulo_ventana', '::. Exportar Comisiones');
		$page_data = array();

		for ($i = 1; $i <= $database->getPages(); $i++) {
			$page_data[] = array(
				'text'  => $language->get('text_pages', $i, $database->getPages()),
				'value' => $i
			);
		}

		$view->set('pages', $page_data);

		return $view->fetch('content/list.tpl');
	}

        function exportar()
	{
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$session  =& $this->locator->get('session');
		set_time_limit(0);
		
		$filtro = 'Filtro:';
		
		$htmlTable="<table border=0 width=180> 
			<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
                        <tr><td colspan=6 align=center style=bold size=14>M.C.G.A.</td></tr>
			<tr><td colspan=6 align=center style=bold size=12>Ciclo lectivo 2011</td></tr>
			<tr><td colspan=6 align=center style=bold size=10>Listado de Comisiones</td></tr>
			<tr><td colspan=6 align=center></td></tr>
			<tr></tr>";

		if (!$session->get('comisiones.cod_comision'))
		{
			$sql = "select cod_comision, descripcion from comisiones";
		}
		else
		{
			$sql = "select cod_comision, descripcion from comisiones where cod_comision like '%" . $session->get('comisiones.cod_comision') . "%' or descripcion like '%" . $session->get('comisiones.cod_comision') . "%'";
		}
		

		$sql .= " order by cod_comision asc";
				
		$reporte = $database->getRows($sql);

		foreach ($reporte as $estu)
		{
			
			$htmlTable .= 	"<tr><td colspan=6>__________________________________________________________________________</td></tr>
							<tr>
								<td align=left size=9>Comisiones:</td>
								<td align=left size=9>" . utf8_decode($estu['cod_comision']) . "</td>
							</tr>
							<tr>
								<td align=left size=9>Descripcion:</td>	
								<td align=left size=9 colspan= 5>" . utf8_decode($estu['descripcion']) . "</td>
							</tr>";				
		}
			 
		$htmlTable .= "</table>";

	define('FPDF_FONTPATH','font/');
	require('javascript/pdf/pdftable.inc.php');
	//ob_end_clean();
	
	$p = new PDFTable();
	$p->AddPage();
	$p->setfont('times','',6);
	$p->htmltable($htmlTable);
	$p->output('','I');
	
	}	
	
	function getForm() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');

		$view = $this->locator->create('template');

		$view->set('heading_title', 'Comisiones');
		$view->set('heading_description','Gesti&oacute;n de Comisiones');

		$view->set('entry_comision', 'Codigo:');
		$view->set('entry_descripcion', 'Comisi&oacute;n:');
                $view->set('entry_cod_materia', 'Materia:');
                $view->set('entry_cupomaximo', 'Cupo m&aacute;ximo:');
		
		
		$view->set('text_enabled', $language->get('text_enabled'));
        $view->set('text_disabled', $language->get('text_disabled'));

		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('error', @$this->error['message']);
		$view->set('error_cod_comision', @$this->error['cod_comision']);
		$view->set('error_descripcion', @$this->error['descripcion']);
                $view->set('error_cod_materia', @$this->error['cod_materia']);
                $view->set('error_cupomaximo', @$this->error['cupomaximo']);
		
		
		$view->set('tab_general', $language->get('tab_general'));

		$view->set('action', $url->ssl('comisiones', $request->get('action'), array('act_codigo' => $request->get('act_codigo'))));
 
		$view->set('cancel', $url->ssl('comisiones'));

		if (($request->get('cod_comision')) && (!$request->isPost())) {
			$consulta = "select distinct * from comisiones where cod_comision = '" . $request->get('cod_comision') . "'";
			$persona_info = $database->getRow($consulta);
		}
		if ($request->has('cod_comision', 'post')) {
			$view->set('cod_comision', $request->get('cod_comision', 'post'));
		} else {
			$view->set('cod_comision', @$persona_info['cod_comision']);
		}
		
		if ($request->has('descripcion', 'post')) {
			$view->set('descripcion', $request->get('descripcion', 'post'));
		} else {
			$view->set('descripcion', @$persona_info['descripcion']);
		}

                if ($request->has('cod_materia', 'post')) {
			$view->set('cod_materia', $request->get('cod_materia', 'post'));
		} else {
			$view->set('cod_materia', @$persona_info['cod_materia']);
		}
		
		$view->set('materias', $database->getRows('Select * from materias'));
                
                if ($request->has('cupomaximo', 'post')) {
			$view->set('cupomaximo', $request->get('cupomaximo', 'post'));
		} else {
			$view->set('cupomaximo', @$persona_info['cupomaximo']);
		}
		
	
		return $view->fetch('content/comisiones.tpl');
	}

	function validateForm() 
	{
		$request  =& $this->locator->get('request');
		$user     =& $this->locator->get('user');
		$language =& $this->locator->get('language');

		if ((strlen($request->get('cod_comision', 'post')) == 0)) {
			$this->error['cod_comision'] = 'Debe ingresar el c&oacute;digo de comis&oacute;n';
		}
		if ((strlen($request->get('descripcion', 'post')) < 2) || (strlen($request->get('descripcion', 'post')) > 100)) {
			$this->error['descripcion'] = 'Debe ingresar el nombre de la comisi&oacute;n';
		}
	/* */	if (($request->get('cod_materia', 'post') == "0" )) {
			$this->error['cod_materia'] = 'Debe seleccionar una Materia';       
		}
                if ((strlen($request->get('cupomaximo', 'post')) == "0" )) {
			$this->error['cupomaximo'] = 'Debe ingresar un cupo';
		}
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function validateDelete() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$config   =& $this->locator->get('config');
		$user     =& $this->locator->get('user');
		$language =& $this->locator->get('language');

			
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

        function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$url      =& $this->locator->get('url');
		$session  =& $this->locator->get('session');

		$session->set('comisiones.cod_comision',$request->get('search','post'));

			
		if (($request->has('page', 'post')) || ($request->has('search', 'post'))) {
			$session->set('comisiones.page', $request->get('page', 'post'));
		}

		if ($request->has('sort', 'post')) {
			$session->set('comisiones.order', (($session->get('comisiones.sort') == $request->get('sort', 'post')) && ($session->get('comisiones.order') == 'asc')) ? 'desc' : 'asc');
		}

		if ($request->has('sort', 'post')) {
			$session->set('comisiones.sort', $request->get('sort', 'post'));
		}

		$response->redirect($url->ssl('comisiones'));
	}	
}
?>